package com.c.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.c.entity.MemberFileReport;
import com.c.service.MemberFileReportService;
import com.c.service.UserService;
import com.c.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @author wangxiangli
 * @create 2022-12-30-20:44
 */
@RestController
@RequestMapping("/memberupload")
public class MemberFileReportController {


    @Autowired
    private MemberFileReportService memberFileReportService;

    @Autowired
    private UserService userService;



    /**
     * 根据页码查找会员体检报告
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        QueryWrapper<com.c.entity.User> wrapper=new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        com.c.entity.User user1 = userService.getOne(wrapper);
        if("健康管理师".equals(user1.getRemark())){
            return memberFileReportService.findByPage(queryPageBean,user1);
        }

        return memberFileReportService.findByPageManger(queryPageBean);
    }

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @PreAuthorize("hasAuthority('FILEREPORT_UPLOAD')")
    @RequestMapping("/uploadFile")
    public Result upload(@RequestParam("file") MultipartFile file) {
        String originalFilename = file.getOriginalFilename();//原始文件名 3bd90d2c-4e82-42a1-a401-882c88b06a1a2.jpg
        int index = originalFilename.lastIndexOf(".");
        String extention = originalFilename.substring(index);//.jpg
        String fileName = UUID.randomUUID().toString() + extention;//	FuM1Sa5TtL_ekLsdkYWcf5pyjKGu.jpg
        try {
            //将文件上传到阿里云服务器
            String fileNameurl = AliyunUtils.upload2Aliyun(file.getBytes(), fileName);
            return new Result(true, MessageConstant.FILE_UPLOAD_SUCCESS, fileNameurl);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.FILE_UPLOAD_FAIL);
    }

    /**
     * 上传体检报告到数据库
     *
     * @param memberFileReport
     * @return
     */
    @RequestMapping("/uploadFileReport")
    public Result uploadFileReport(@RequestBody MemberFileReport memberFileReport) {
        try {
            return memberFileReportService.uploadFileReport(memberFileReport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBERFILEREPORT_FAIL);
        }
    }

    /**
     * 根据ID查询体检信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            MemberFileReport memberFileReport = memberFileReportService.getById(id);
            return new Result(true, MessageConstant.QUERY_CMEMBERFILEREPORT_SUCCESS, memberFileReport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBERFILEREPORT_FAIL);
        }
    }


    /**
     * 删除体检报告
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('FILEREPORT_DELETE')")
    @RequestMapping("/deleteFileReport")
    public Result deleteFileReport(Integer id) {
        try {
            memberFileReportService.deleteFileReport(id);
            return new Result(true, MessageConstant.DEL_MEMBERFILEREPORT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DEL_MEMBERFILEREPORT_FAIL);
        }
    }
}
